/*
 * 91.cpp
 *
 *  Created on: Feb 13, 2014
 *      Author: killerg
 */




#include "../bill_deps.h"
#include "platform_deps.h"

#include <list>
#include <map>
#include <string>

static const int AppId = 110889;
static const char* AppKey = "1c8f4d6c09134198a982c5a50f4f30ff61d08ec1dfa7a117";
static const int Act = 1;

static void send_result(
        http_server::p_request req,
        int code,
        const char* msg) {

    Json::Value ret;
    ret["ErrorCode"] = code;
    ret["ErrorDesc"] = msg;

    if (code != 1) {
        auto uri = evhttp_request_get_evhttp_uri(req->req);
        auto query = evhttp_uri_get_query(uri);
        char buff[100];
        time_t now = time(NULL);
        strftime(buff,100,"%Y%m%d_%H%M%S",localtime(&now));

    	UTIL_LOG(E, "%s EPI_91 errcode:%d, msg:%s, query:%s", buff, code, msg, query);
    }

    req->send_reply(200, to_cstr(ret));
}

bool init_91() {
    g_https->handle("/bill/91", [](http_server::p_request req){
    	if (g_svrmgr->db_set >= 100) {
    		send_result(req, 0, "retry later");
        	return;
    	}


#define GETARG(arg, errcode) \
    	string s##arg = req->query[#arg];	\
    	if (s##arg.empty()) {					\
    		send_result(req, errcode, "invalid "#arg);	\
    		return;								\
    	}

    	GETARG(AppId, 2);
    	GETARG(Act, 3);
    	GETARG(ProductName, 4);
    	GETARG(ConsumeStreamId, 4);
    	GETARG(CooOrderSerial, 4);
    	GETARG(Uin, 4);
    	GETARG(GoodsId, 4);
    	GETARG(GoodsInfo, 4);
    	GETARG(GoodsCount, 4);
    	GETARG(OriginalMoney, 4);
    	GETARG(OrderMoney, 4);
    	GETARG(Note, 4);
    	GETARG(PayStatus, 4);
    	GETARG(CreateTime, 4);
    	GETARG(Sign, 5);

        if (sPayStatus != "1") {
            send_result(req, 1, "success");
            return;
        }

    	string toSign = fmt_cstr("%d%d%s%s%s%s%s%s%s%s%s%s%s%s%s",
    			AppId,
    			Act,
    			sProductName.c_str(),
    			sConsumeStreamId.c_str(),
    			sCooOrderSerial.c_str(),
    			sUin.c_str(),
    			sGoodsId.c_str(),
    			sGoodsInfo.c_str(),
    			sGoodsCount.c_str(),
    			sOriginalMoney.c_str(),
    			sOrderMoney.c_str(),
    			sNote.c_str(),
    			sPayStatus.c_str(),
    			sCreateTime.c_str(),
    			AppKey
    			);

    	string calced = Md5(toSign.c_str());
    	if (calced != sSign) {
    		send_result(req, 5, "sign err");
    		UTIL_LOG(E, "toSign:%s, calced:%s, sign:%s", toSign.c_str(), calced.c_str(), sSign.c_str());
    		return;
    	}

    	string account;
        string orderId;
        double payFee = 0;
        int serverid = -1;
        string machinecode;

        account = fmt_cstr("91-%s", sUin.c_str());
        orderId = fmt_cstr("91-%s", sCooOrderSerial.c_str());
        payFee = atof(sOrderMoney.c_str()) * g_cfg->Rate;

        std::vector<std::string> vNote;
        split(sNote, ",", vNote);
		if (vNote.size() != 2) {
            send_result(req, 4, "Note error");
            return;
		}
		serverid = atoi(vNote[0].c_str());
		machinecode = vNote[1];

        if (payFee <= 0) {
            send_result(req, 4, "OrderMoney error");
            return;
        }

        if (serverid <= 0) {
            send_result(req, 4, "Note error");
            return;
        }

        auto playerinfo = g_svrmgr->get_player_info(serverid, account);
        if (NULL == playerinfo) {
        	send_result(req, 0, "no player info");
            return;
        }

        g_svrmgr->post_order(serverid, orderId, playerinfo->playerid, payFee, machinecode, [req](bool ok){
            if (ok)
            	send_result(req, 1, "success");
            else
            	send_result(req, 0, "err");
        });

    });

    return true;
}
